package com.miplus.ccrm.core.service.crd;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.common.JUsrPerm;
import com.miplus.ccrm.core.controller.vo.credit.CrdBillTailResVo;
import com.miplus.ccrm.core.controller.vo.credit.CrdNOBillTailReqVo;
import com.miplus.ccrm.core.controller.vo.credit.CrdNOBillTailResVo;
import com.miplus.ccrm.core.controller.vo.credit.CrdNOutBillResVo;
import com.miplus.ccrm.core.mapper.CreditAccountMapper;
import com.miplus.ccrm.core.pojo.CreditAccount;
import com.miplus.ccrm.core.pojo.CreditAccountNoutbill;
import com.miplus.ccrm.core.mapper.CreditAccountNoutbillMapper;
import com.miplus.ccrm.core.pojo.CreditAccountOutbill;
import com.miplus.ccrm.core.pojo.CreditCard;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
* @author pc
* @description 针对表【credit_account_noutbill(信用卡账单未出账单明细)】的数据库操作Service实现
* @createDate 2024-04-29 21:55:28
*/
@Service
public class CreditAccountNoutbillServiceImpl extends MPJBaseServiceImpl<CreditAccountNoutbillMapper, CreditAccountNoutbill>
    implements CreditAccountNoutbillService{

    @Resource
    private CreditAccountMapper creditAccountMapper;

    @Override
    public CrdNOutBillResVo crdNOutBill(String acctno)
    {
        CreditAccount account=creditAccountMapper.selectOne(
                new QueryWrapper<CreditAccount>()
                        .eq("acct",acctno)
        );
        String billdaystr="",paydaystr="";
        Date billday=new Date();
        Date payday=new Date();
        int intbillday= JPubUtils.StringToInt(account.getBillDate());
        //2024-07-26 还款日是具体的日期，比如2024-03-25,富邦还款日为空时就是账单日+20天
        int intpayday=0;
        if(!JPubUtils.IsEmpty(account.getRepayDate())){
            intpayday=JPubUtils.StringToInt(account.getRepayDate().substring(8));
        }
        Calendar calendar=Calendar.getInstance();
        int dom=calendar.get(Calendar.DAY_OF_MONTH); //取本月第几天
        if(dom>=intbillday){
            billday=JPubUtils.addMonths(billday,1);
        }
        if(intpayday==0){
            payday=JPubUtils.addDays(billday,20);
            calendar.setTime(payday);
            intpayday=calendar.get(Calendar.DAY_OF_MONTH);
        }
        else if(dom>=intpayday){
            payday=JPubUtils.addMonths(payday,1);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        billdaystr=String.format("%s%02d",sdf.format(billday),intbillday);
        paydaystr=String.format("%s%02d",sdf.format(payday),intpayday);

        CrdNOutBillResVo resVo=new CrdNOutBillResVo();
        resVo.setAcct(acctno);
        resVo.setBillDate(billdaystr);
        resVo.setRepayDate(paydaystr);

        List<CreditAccountNoutbill> list=baseMapper.selectList(
                new QueryWrapper<CreditAccountNoutbill>()
                        .eq("acct",acctno)
        );
        double costamt=0.0;
        double payamt=0.0;
        for(CreditAccountNoutbill noutbill : list)
        {
            if(JPubUtils.IsEqual(noutbill.getDbCrInd(),"+")||
                    JPubUtils.IsEqual(noutbill.getDbCrInd(),"D")){
                if(noutbill.getBillAmt()!=null) {
                    costamt += noutbill.getBillAmt().doubleValue();
                }
            }
            if(JPubUtils.IsEqual(noutbill.getDbCrInd(),"-")||
                    JPubUtils.IsEqual(noutbill.getDbCrInd(),"C")){
                if(noutbill.getBillAmt()!=null) {
                    payamt += noutbill.getBillAmt().doubleValue();
                }
            }
        }
        resVo.setCostAmt(String.format("%.2f",costamt));
        resVo.setPayAmt(String.format("%.2f",payamt));

        return resVo;
    }

    @Override
    public IPage<CrdNOBillTailResVo> crdNOutBillList(CrdNOBillTailReqVo paras)
    {
        return selectJoinListPage(new Page<>(paras.getPageNum(), paras.getPageSize()),
                CrdNOBillTailResVo.class,
                new MPJLambdaWrapper<CreditAccountNoutbill>()
                        .selectAll(CreditAccountNoutbill.class)
                        .eq(CreditAccountNoutbill::getAcct,paras.getAcctNo())
                        .orderByDesc(CreditAccountNoutbill::getInpDate,CreditAccountNoutbill::getInpTime)
        );
    }
}




